package com.zryc.shop.sms.controller;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.zryc.shop.sms.entity.FlashPromotionSession;
import com.zryc.shop.sms.service.IFlashPromotionProductRelationService;
import com.zryc.shop.sms.service.IFlashPromotionSessionService;
import com.zryc.shop.util.ResponseResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.bouncycastle.cert.ocsp.Req;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.stereotype.Controller;

import java.time.LocalDateTime;

/**
 * <p>
 * 限时购场次表 前端控制器
 * </p>
 *
 * @author LiYe
 * @since 2023-02-13
 */
@RestController
@RequestMapping("/sms/flashSession")
@Api(tags = "秒杀时间段管理接口列表")
public class FlashPromotionSessionController {
    @Autowired
    private IFlashPromotionSessionService sessionService;

    @Autowired
    private IFlashPromotionProductRelationService relationService;

    @GetMapping("/selectList")
    @ApiOperation("查询活动对应的时间段列表")
    public ResponseResult selectList(@RequestParam Long flashPromotionId) {
        return new ResponseResult(200, "查询成功", relationService.listByFlashPromotion(flashPromotionId));
    }

    @GetMapping("/list")
    @ApiOperation("查询列表")
    public ResponseResult list() {
        return new ResponseResult(200, "查询成功", sessionService.list());
    }

    @PostMapping("/create")
    @ApiOperation("添加秒杀时间段")
    public ResponseResult create(@RequestBody FlashPromotionSession session) {
        session.setCreateTime(LocalDateTime.now());
        sessionService.save(session);
        return new ResponseResult(200, "创建成功");
    }

    @PostMapping("/update/{id}")
    @ApiOperation("更新秒杀时间段")
    public ResponseResult update(@RequestBody FlashPromotionSession session, @PathVariable Long id) {
        session.setId(id);
        sessionService.updateById(session);
        return new ResponseResult(200, "修改成功");
    }

    @PostMapping("/delete/{id}")
    @ApiOperation("删除秒杀时间段")
    public ResponseResult delete(@PathVariable Long id) {
        sessionService.removeById(id);
        return new ResponseResult(200, "删除成功");
    }

    @PostMapping("/update/status/{id}")
    @ApiOperation("修改状态")
    public ResponseResult updateStatus(@PathVariable Long id, Integer status) {
        UpdateWrapper updateWrapper = new UpdateWrapper();
        updateWrapper.eq("id", id);
        updateWrapper.set("status", status);
        sessionService.update(updateWrapper);
        return new ResponseResult(200, "修改成功");
    }


}
